// Planet API.
//
// Planet API是Planet提供的HTTP API。
//
// 		Host: localhost
//     	Version: 0.0.1
//
// swagger:meta
package gateway

import (
	"context"
	"github.com/Unixeno/TheMoon/logging"
	"github.com/Unixeno/TheMoon/planet/config"
	localCtx "github.com/Unixeno/TheMoon/planet/core/context"
	"net/http"
)

const APIVersion = "1.0"

var gatewayServer http.Server
var logger = logging.GetLogger("gateway")

func Start() {
	go func() {
		defer gatewayServer.Close()
		<-localCtx.Done()
	}()
	gatewayServer.Addr = config.SysConfig.Gateway.Addr
	http.Handle("/", NewRouter())
	logger.WithField("addr", gatewayServer.Addr).Debug("starting gateway...")
	go gatewayServer.ListenAndServe()
}

func Shutdown() error {
	ctx, _ := context.WithTimeout(context.Background(), 5)
	return gatewayServer.Shutdown(ctx)
}
